class UnionFind:
    def __init__(self, num):
        self.fa = list(range(num + 1))

    def find(self, x):
        if self.fa[x] != x:
            self.fa[x] = self.find(self.fa[x])
        return self.fa[x]

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x != root_y:
            self.fa[root_y] = root_x


# 请在此输入您的代码
m, n = map(int, input().split())
k = int(input())
uf = UnionFind(m * n)
for _ in range(k):
    u, v = map(int, input().split())
    uf.union(u, v)
result = 0
for i in range(1, m * n + 1):
    if uf.find(i) == i:
        result += 1
print(result)
